package com.dgut.admodule.entity.dto;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;

import com.dgut.admodule.entity.Ad;

public class AdQueryDTO {
	private Long adId;
	private String adsName;
	private String adsDescription;
	@DateTimeFormat(pattern="yyyy/MM/dd HH:mm:ss")
	private Date adsbTime;//开始时间
	@DateTimeFormat(pattern="yyyy/MM/dd HH:mm:ss")
	private Date adssTime;//结束时间
	private String adsCompany;
	
	public Long getAdId() {
		return adId;
	}
	public void setAdId(Long adId) {
		this.adId = adId;
	}
	public String getAdsName() {
		return adsName;
	}
	public void setAdsName(String adsName) {
		this.adsName = adsName;
	}
	public String getAdsDescription() {
		return adsDescription;
	}
	public void setAdsDescription(String adsDescription) {
		this.adsDescription = adsDescription;
	}
	public Date getAdsbTime() {
		return adsbTime;
	}
	public void setAdsbTime(Date adsbTime) {
		this.adsbTime = adsbTime;
	}
	public Date getAdssTime() {
		return adssTime;
	}
	public void setAdssTime(Date adssTime) {
		this.adssTime = adssTime;
	}
	public String getAdsCompany() {
		return adsCompany;
	}
	public void setAdsCompany(String adsCompany) {
		this.adsCompany = adsCompany;
	}
	
	public static Specification<Ad> getSpecification(AdQueryDTO adQueryDTO)
	{
		Specification<Ad> spec = new Specification<Ad>() {
			public Predicate toPredicate(Root<Ad> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				 if(null != adQueryDTO && !StringUtils.isEmpty(adQueryDTO.getAdId())) {
					 Predicate  p1 =  cb.equal(root.get("adId"),adQueryDTO.getAdId());
					 list.add(p1);
				 }
				 if(null != adQueryDTO && !StringUtils.isEmpty(adQueryDTO.getAdsName())) {
					 Predicate  p2 =  cb.like(root.get("adsName").as(String.class),"%"+ adQueryDTO.getAdsName() + "%");
					 list.add(p2);
				 }
				 if(null != adQueryDTO && !StringUtils.isEmpty(adQueryDTO.getAdsDescription())) {
					 Predicate  p3 =  cb.like(root.get("adsDescription").as(String.class),"%"+ adQueryDTO.getAdsDescription() + "%");
					 list.add(p3);
				 }
				 if(null != adQueryDTO && !StringUtils.isEmpty(adQueryDTO.getAdsCompany())) {
					 Predicate  p4 =  cb.like(root.get("adsCompany").as(String.class),"%"+ adQueryDTO.getAdsCompany() + "%");
					 list.add(p4);
				 }
				 
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;
	}
}
